<?php

declare (strict_types = 1);

namespace app\controller\admin\system;

use app\BaseController;
use app\service\admin\file\UploadService;

class Upload extends BaseController
{
    protected $fileService;

    public function initialize()
    {
        $this->fileService = make(UploadService::class);
    }
    /**
     * 图片文件上传（单文件）
     */
    public function uploadImage()
    {
        $this->validate($this->request->file(), [
            'file' => 'file|fileExt:jpg,png,jpeg|fileSize:10485760'
        ],[
            'file.file' => '请选上传文件', 'file.fileExt'=> '系统仅支持 jpg,png,jpeg 图片格式',
            'file.fileSize' => '可上传最大文件10M'
        ]);
        $path = $this->request->param('type') ?? 'image';
        $file = $this->request->file('file');
        return $this->success($this->fileService->upload($file, $path));
    }
    /**
     * 附件文件上传（单文件）
     */
    public function uploadFile()
    {
        $this->validate($this->request->file(), [
            'file' => 'file|fileExt:xls,xlsx,mp4|fileSize:10485760'
        ],[
            'file.file' => '请选上传文件', 'file.fileExt'=> '系统仅支持 xls,xlsx,mp4 格式文件',
            'file.fileSize' => '可上传最大文件10M'
        ]);
        $path = $this->request->param('type') ?? 'file';
        $file = $this->request->file('file');
        return $this->success($this->fileService->upload($file, $path));
    }
}
